﻿#include "OperVAOnePostDataImport.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppSettings.h"
#include "FITK_Kernel/FITKAppFramework/FITKComponents.h"
#include "FITK_Kernel/FITKCore/FITKAbstractDataObject.h"
#include "FITK_Kernel/FITKCore/FITKThreadPool.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include "FITK_Component/FITKVAOneHDF5IO/FITKVAOneHDF5IOInterface.h"
#include "FITK_Component/FITKAcousticsPostData/FITKAcousticsPostData.h"
#include <QFile>
#include <QFileDialog>
#include <QTextStream>

namespace ModelOper
{

    bool OperVAOnePostDataImport::execGUI()
    {
        //清理参数数据
        this->clearArgs();
        //主界面
        QWidget* mainWindow = FITKAPP->getGlobalData()->getMainWindow();
        if (!mainWindow || !_emitter) return false;
        //获取工作目录
        QString workDir;
        AppFrame::FITKAppSettings* settings = FITKAPP->getAppSettings();
        if (settings)
            workDir = settings->getWorkingDir();

        //获取并设置导入类型,创建导入窗口
        QString actionName = _emitter->objectName();
        QString title, filter;
        if (actionName == "actionHDF5Result")
        {
            this->setArgs("FileType", "HDF5");
            title = tr("Import HDF5 File");
            filter = "HDF5(*.hdf5)";
        }
        QFileDialog dialog(mainWindow, title, workDir, filter);
        dialog.setAcceptMode(QFileDialog::AcceptOpen);  //打开模式
        dialog.show();
        //阻塞，否则键盘事件处理会出问题
        bool accept = false;
        QEventLoop loop;
        connect(&dialog, &QFileDialog::accepted, [&] {loop.quit(); accept = true;  });
        connect(&dialog, &QFileDialog::rejected, [&] {loop.quit(); accept = false;  });
        loop.exec();
        if (!accept) return false;
        //获取选择的文件名称
        QStringList files = dialog.selectedFiles();
        if (files.isEmpty()) return false;
        QString fileName = files.at(0);
        if (fileName.isEmpty())
            return false;
        //设置读取的文件路径
        this->setArgs("FileName", fileName);
        return true;
    }

    bool OperVAOnePostDataImport::execProfession()
    {
        QString type;
        if (!this->argValue<QString>("FileType", type)) return false;
        bool okProfession = false;
        if (type == "HDF5")
            okProfession = this->importHDF5ResultOper();
        return okProfession;
    }

    bool OperVAOnePostDataImport::importHDF5ResultOper()
    {
        //导入文件名
        QString fileName;
        if (!this->argValue<QString>("FileName", fileName) || fileName.isEmpty()) return false;
        //读写组件获取
        VAOne::FITKVAOneHDF5IOInterface* hdf5IO = FITKAPP->getComponents()->
            getComponentTByName<VAOne::FITKVAOneHDF5IOInterface>("VAOneHDF5IO");

        //等待线程结束
        Core::FITKThreadPool::getInstance()->wait();

        if (nullptr == hdf5IO) return false;

        //清除后处理数据
        clearData();

        //获取要存储的数据位置
        Core::FITKAbstractDataObject* postData =
            FITKAPP->getGlobalData()->getPostData<Core::FITKAbstractDataObject>();

        hdf5IO->setDataObject("postData", postData);
        hdf5IO->setFileName(fileName);
        hdf5IO->runInThread();
        //关联结束读取槽函数
        connect(hdf5IO, SIGNAL(ioThreadFinishedSig()), this, SLOT(ioThreadFinishedSlot()), Qt::UniqueConnection);
        //执行读取
        hdf5IO->exec(1);

        //保存执行的脚本
        QString scr = QString("Files.ImportPostDataFile('%1')").arg(fileName);
        this->saveScript(scr);
        return hdf5IO->isSuccess();
    }

    void OperVAOnePostDataImport::ioThreadFinishedSlot()
    {
        VAOne::FITKVAOneHDF5IOInterface* hdf5IO = FITKAPP->getComponents()->
            getComponentTByName<VAOne::FITKVAOneHDF5IOInterface>("VAOneHDF5IO");
        if (hdf5IO == nullptr) return;
        //取消关联
        disconnect(hdf5IO, SIGNAL(ioThreadFinishedSig()), this, SLOT(ioThreadFinishedSlot()));

    }

    void OperVAOnePostDataImport::clearData()
    {
        Acoustics::FITKAcousticsPostData* postData =
            FITKAPP->getGlobalData()->getPostData<Acoustics::FITKAcousticsPostData>();

        if (postData) postData->clearData();
    }
}

